package com.sdp.wx.controller;


import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_vehicle_wz.entity.WxRepairVehicleWz;
import com.sdp.wx.db.wx_repair_vehicle_wz.service.IWxRepairVehicleWzService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-19
 */
@RestController
@RequestMapping("/WxRepairVehicleWzController")
public class WxRepairVehicleWzController {

	@Autowired
	private IWxRepairVehicleWzService iWxRepairVehicleWzService;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairVehicleWzService, WxRepairVehicleWz.class, dataCenter));
	}
	
	/**
	 * 查询需要出库的物资
	 * @param dataCenter
	 * @return
	 */
	@PostMapping(value = "/queryCanOutputWz")
	public Result<?> queryCanOutputWz(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WxRepairVehicleWz> queryWrapper = new QueryWrapper<>();
		queryWrapper.apply("output_num<pick_num");
		return Result.ok(query.list(iWxRepairVehicleWzService, WxRepairVehicleWz.class, dataCenter,queryWrapper));
	}
	
	/**
	 * 添加零配件
	 * @param items
	 * @return
	 */
	@PostMapping(value = "/addWz")
	@Transactional(rollbackFor=Exception.class)
	public synchronized Result<?> addItem(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("器材信息不存在");
		}
		List<WxRepairVehicleWz> items = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxRepairVehicleWz.class);
		String repairCode = obj.getString("repairCode");
		if(StringUtils.isEmpty(repairCode)) {
			return Result.error("维修编号不存在");
		}
		
		if(items==null || items.size()==0) {
			return Result.error("器材信息不存在");
		}
		for(WxRepairVehicleWz item : items) {
			item.setKeyId(null);
			item.setRepairCode(repairCode);
			item.setPickNum(BigDecimal.ONE);
			item.setOutputNum(BigDecimal.ZERO);
			item.setNotOutputNum(BigDecimal.ONE);
		}
		
		List<WxRepairVehicleWz> dbItems = iWxRepairVehicleWzService.queryWxRepairVehicleWzByRepairCode(repairCode);
		if(dbItems==null || dbItems.size()==0) {
			iWxRepairVehicleWzService.saveBatch(items);
		}else {
			Iterator<WxRepairVehicleWz> iterator = items.iterator();
			for(WxRepairVehicleWz dbItem : dbItems) {
				if(items.size()==0) {
					break;
				}
				iterator = items.iterator();
				while (iterator.hasNext()) {
					if(iterator.next().getWzNo().equals(dbItem.getWzNo())) {
						iterator.remove();
						break;
					}
				}
			}
			
			if(items.size()>0) {
				iWxRepairVehicleWzService.saveBatch(items);
			}
		}
		return Result.ok();
	}
	
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxRepairVehicleWzService.removeById(keyId);
		return Result.ok();
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxRepairVehicleWz repairVehicleWz) {
		if(repairVehicleWz==null) {
			return Result.error("维修领料信息不存在");
		}
		repairVehicleWz.setNotOutputNum(repairVehicleWz.getPickNum().subtract(repairVehicleWz.getOutputNum()));
		iWxRepairVehicleWzService.updateById(repairVehicleWz);
		return Result.ok();
	}
}

